package com.apexedu.portal.service.impl;

import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.portal.action.PortalServiceProblemAction;
import com.apexedu.portal.entity.TAnswerPro;
import com.apexedu.portal.entity.TProblem;
import com.apexedu.util.DateUtil;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by janabo on 2015-1-15.
 */
@Service("portalServiceProblemService")
public class PortalServiceProblemServiceImpl extends AbstractBaseServiceSupport {
    @Override
    public void saveOrUpdate(Object o) throws Exception {
        PortalServiceProblemAction action = (PortalServiceProblemAction)o;
        TAnswerPro discuss = action.getDiscuss();
        discuss.setPtime(DateUtil.formatDate(new Date()));
        discuss.setPuser(action.getUserloginid());
        discuss.setPid(action.getPid());
        if(StringUtil.isBlank(discuss.getWid())){
            discuss.setWid(SeqFactory.getNewSequenceAlone());
        }
        getBaseDao().saveOrUpdate(discuss);
        TProblem tp =  DBUtil.queryBean("select * from T_PROBLEM t where t.wid = ?",TProblem.class,action.getPid());
        action.setProblem(tp);
    }

    @Override
    public void load(Object o) throws Exception {
        PortalServiceProblemAction action = (PortalServiceProblemAction)o;
        String wid = action.getWid();
        TProblem tp =  DBUtil.queryBean("select * from T_PROBLEM t where t.wid = ?",TProblem.class,wid);
        TAnswerPro discuss =  DBUtil.queryBean("select * from t_answerpro t where t.pid = ?", TAnswerPro.class, wid);
        action.setDiscuss(discuss);
        action.setPid(wid);
        action.setProblem(tp);
    }

    @Override
    public  List<TProblem> list(Object o, Pager pager) throws Exception {
        PortalServiceProblemAction action = (PortalServiceProblemAction)o;
        TProblem bean = action.getProblem();
        List<Object> params = new LinkedList<Object>();
        String sql = "select t.*,case  when d.wid is not null THEN '1' else '0' end as state from T_PROBLEM t " +
                "left join  t_answerpro d on t.wid = d.pid where 1 = 1";
        StringBuffer sb = new StringBuffer(sql);
        if(bean != null){
            if(StringUtil.isNotBlank(bean.getPcontent())){//标题或内容进行模糊查询
                sb.append(" and (t.pproblem like ? or t.pcontent like ?)");
                params.add("%" + bean.getPcontent() + "%");
                params.add("%" + bean.getPcontent() + "%");
            }
            if(StringUtil.isNotBlank(bean.getZymc())){//资源名称模糊查询
                sb.append(" and t.zymc like ?");
                params.add("%" + bean.getZymc() + "%");
            }
            if(StringUtil.isNotBlank(bean.getState())) {//资源名称模糊查询
                if("1".equals(bean.getState())) {
                    sb.append(" and d.wid is not null");
                }else{
                    sb.append(" and d.wid is null");
                }
            }
        }
        sb.append(" order by t.ptime desc");
        pager.setTotalRows(DBUtil.count("select count(*) from (" + sb.toString() + ")", params.toArray()));
        pager.setEachPageRows(10);
        List<TProblem> list = DBUtil.queryPageBeanList(pager, sb.toString(), TProblem.class,params.toArray());
        return list;
    }

    @Override
    public boolean remove(Object o) throws Exception {
        return false;
    }
}
